//
//  ViewController.m
//  Movie
//
//  Created by cjy on 16/3/21.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "ViewController.h"
#import "NetworkHandeler.h"
#import "CollectionCellForFirst.h"
#import "CollectionCellForHot.h"
#import <UIImageView+WebCache.h>
#import "MovieModel.h"
#import "VCForPushAVPlayer.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

#define WIDTH2 [UIScreen mainScreen].bounds.size.width
#define HEIGHT2 [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate,CellForHotDelegate>

@property (nonatomic, strong)NSMutableArray *arrayTitle;
@property (nonatomic, strong)NSMutableArray *arrayNum;

@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UIView *viewForline;
@property (nonatomic, strong)UICollectionView *collectView;
@property (nonatomic, strong)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong)UIButton *button;
@property (nonatomic, strong)UIButton *buttonForRed;
@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic, strong)NSString *stringT;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"CT"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
 
    self.navigationItem.title = @"JuJu";
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont fontWithName:@"Zapfino" size:16];
    label.shadowOffset = CGSizeMake(0.5, 1);
    label.shadowColor = [UIColor colorWithWhite:1 alpha:1];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor colorWithRed:0.971 green:0.893 blue:0.871 alpha:1.000];
    label.text = self.navigationItem.title;
    self.navigationItem.titleView = label;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.array = [NSMutableArray array];  //存放 idnum 数值的数组
    self.arrayNum = [NSMutableArray array];
    self.arrayTitle = [NSMutableArray array];
    
    self.view.backgroundColor = [UIColor colorWithRed:28 / 255.f green:28 / 255.f blue:28 / 255.f alpha:1];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];  // NavigationBaritem 颜色

    [self createCollectionView];
    [self createKVOAction];
    [self createScrollView];
    
}


- (void)createScrollView{
    
    self.arrayTitle = @[@"热门", @"爱情", @"搞笑", @"创意", @"人生",@"剧情",@"青春",@"励志",@"摄影",@"MV"].mutableCopy;
    self.arrayNum = @[@"0",@"12",@"8",@"6",@"5",@"17",@"29",@"7",@"11",@"18"].mutableCopy;

    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, 40)];
    
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    self.scrollView.backgroundColor = [UIColor colorWithRed:80 / 255.f green:80 / 255.f blue:80 / 255.f alpha:1];
    
    self.scrollView.tintColor = [UIColor whiteColor];
    
    [self.view addSubview:self.scrollView];
    
    self.scrollView.contentSize = CGSizeMake(WIDTH / 5 * self.arrayTitle.count, 0);
    
    
    for (int i = 0; i < self.arrayTitle.count; i++) {
        
        self.button = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.button setTitle:self.arrayTitle[i] forState:UIControlStateNormal];
        
        self.button.frame = CGRectMake(WIDTH / 5 *  i , 10, WIDTH / 5 - 10, 30);
        self.button.tag = i + 1;
        
        [self.button addTarget:self action:@selector(handleOfbutton:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.scrollView addSubview:self.button];
    }
    
    [self createLine];
}


- (void)createLine{
    
    self.viewForline = [[UIView alloc]initWithFrame:CGRectMake(0, 100, WIDTH / 5 - 10, 2)];
    
    self.viewForline.backgroundColor = [UIColor colorWithRed:244 / 255.f green:255 / 255.f blue:255 / 255.f alpha:0.8];
//    self.viewForline.alpha = 0.4f;
    
    [self.view addSubview:self.viewForline];
}


- (void)handleOfbutton:(UIButton *)button{
    
    NSIndexPath *path = [NSIndexPath indexPathForItem:button.tag - 1 inSection:0];
    
    [self.collectView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"passItem" object:nil userInfo:@{@"index":[NSString stringWithFormat:@"%ld", button.tag - 1 ]}];   //
}


- (void)createCollectionView{
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT2 - 100);
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //collection
    self.collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 50, WIDTH, HEIGHT2 - 30) collectionViewLayout:self.flowLayout];
    [self.view addSubview:self.collectView];
    self.collectView.dataSource = self;
    self.collectView.delegate = self;
    self.collectView.pagingEnabled = YES;
    self.collectView.showsVerticalScrollIndicator = NO;
    self.collectView.showsHorizontalScrollIndicator = NO;
    
    [self.collectView registerClass:[CollectionCellForHot class] forCellWithReuseIdentifier:@"poolForCell"];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.arrayTitle.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    CollectionCellForHot *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCell" forIndexPath:indexPath];
    
  
    self.stringT = self.arrayNum[indexPath.item];
    
    cell.delegate = self;
    cell.backgroundColor = [UIColor blackColor];
    [cell passData:self.stringT];
    
    return cell;
    
}


#pragma mark - CellForpicUrlCollectionViewCell的协议方法
- (void)didClickSelect:(MovieModel *)model {
    
    VCForPushAVPlayer *show = [[VCForPushAVPlayer alloc]init];

    show.idForPush = model.postid;
    show.titleForPush = model.title;
    
    [self.navigationController pushViewController:show animated:NO];
}


#pragma mark - 知识点1 KVO(键值观察者)
/** 核心: 观察者 观察 某一个对象的属性变化 */
- (void)createKVOAction{
    
    //VC 观察 collection 的 collectionOffset 属性
    [self.collectView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:@"context"];
}


/** 当被观察者属性发生变化的时候,调用此方法 */   // KVO
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *, id> *)change context:(void *)context{
    
    CGFloat x = [[change objectForKey:@"new"]CGPointValue].x;
    CGFloat redX = x / 5 ;

    
    if (x <= WIDTH * 2) {
        
        [UIView animateWithDuration:0.2 animations:^{
            
            self.scrollView.contentOffset = CGPointMake(0, 0);
        }];
        
        // 修改 RedLine 位置
        self.viewForline.transform = CGAffineTransformMakeTranslation(redX, 0);
        
    } else if (x > (self.arrayTitle.count - 3) * WIDTH) {
        
        self.scrollView.contentOffset = CGPointMake(WIDTH / 5 * 5, 0);
        // 修改 RedLine 位置
        self.viewForline.transform = CGAffineTransformMakeTranslation((x - 5 * WIDTH) / 5, 0);
        
    } else {
        
        [UIView animateWithDuration:0.2 animations:^{
            
            self.scrollView.contentOffset = CGPointMake((x - 2 * WIDTH) / 5, 0);
        }];
        
        self.viewForline.transform = CGAffineTransformMakeTranslation(WIDTH * 2 / 5, 0);
    }
    
    int b = (int)x / WIDTH + 1;
    
    if (b >= 1) {
        
        UIButton *button = [self.view viewWithTag:b];
        
        [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
        self.button = button;
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
